﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Transactions;
using IssueTracker.Data;
using IssueTracker.Business.Entity;

namespace IssueTracker.Business.BO
{
    public class IssueAttachmentBO
    {

  
        private IssueAttachmentDataAccess da;

        public IssueAttachmentBO() 
        {
            da = new IssueAttachmentDataAccess();
        }


#region Business Methods

        /// <summary>
        /// Add a new IssueAttachment
        /// </summary>
        /// <param name="issueAttachment"></param>
        /// <returns></returns>
        public IssueAttachment AddNew(IssueAttachment issueAttachment)
        {
            IssueAttachment returnIssueAttachment;

            issueAttachment.CreationDate = DateTime.Today.Date;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnIssueAttachment = da.Create(issueAttachment);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }

            return returnIssueAttachment;

        }

        public IssueAttachment UpdateIssueAttachment(IssueAttachment issueAttachment)
        {
            IssueAttachment returnIssueAttachment;

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    returnIssueAttachment = da.Update(issueAttachment);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                
                return returnIssueAttachment;
            }
        }

        public void DeleteIssueAttachment(IssueAttachment issueAttachment)
        {
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    da.Delete(issueAttachment);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<IssueAttachment> ListIssueAttachments()
        {
            IssueAttachmentDataAccess data = new IssueAttachmentDataAccess();

            IEnumerable<IssueAttachment> returnList = data.List();
            return returnList;
        }

        public IssueAttachment GetIssueAttachment(System.Guid id)
        {
            return da.Retrieve(id);
        }

        public IEnumerable<IssueAttachment> ListIssueAttachementsByRef(System.Guid reference)
        {
            return da.ListByReferenceId(reference);
        }

#endregion
        
   
    }
}
